package com.example.syyao.myapp.Personal;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.syyao.myapp.Adapter.personal_heart_adapter;
import com.example.syyao.myapp.MainActivity;
import com.example.syyao.myapp.MainFragment;
import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Response.Get_response;
import com.example.syyao.myapp.Response.Get_response_List;
import com.example.syyao.myapp.Sign.Sign_in;
import com.example.syyao.myapp.WebContact.JsonUtil;
import com.example.syyao.myapp.WebContact.WebAccessTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;


public class Personal_heart extends SwipeBackActivity {
    private LinearLayout btn_back;
    private Get_response_List conner_response;
    List<Map<String,Object>> arrayList;
    personal_heart_adapter pla;
    ListView list;

    //列表
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.persoal_heart);
        overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        LinearLayout btn_back=(LinearLayout) findViewById(R.id.lay_back);
        btn_back.setOnClickListener(new Btnback());

        list=(ListView) findViewById(R.id.heartlist);

        getData();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0,R.anim.slide_out_right);
    }

    private class Btnback implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            finish();
        }
    }
    //ArrayList<HashMap<String, Object>>
    private void getData() {
        SharedPreferences sharedPreferences = getSharedPreferences(MainActivity.PREFERENCE_NAME,MainActivity.MODE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        final String username = sharedPreferences.getString("username","1");
        new Thread(new Runnable() {

            @Override
            public void run() {
//                System.out.println(username);
                String webContent = WebAccessTools.Get_Conner_Lsit(username);
                System.out.println("web:"+webContent);
                conner_response= (Get_response_List) JsonUtil.jsonToObject(webContent,
                        Get_response_List.class);
                handler.sendEmptyMessage(0x123);
            }
        }).start();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            List<Map<String,Object>> temp = new ArrayList<>();
            if (conner_response != null && conner_response.getMessage().equals("成功")) {
                arrayList = new ArrayList<>();
                temp = conner_response.getData();
                for (int i = 0;i < temp.size();i++) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("personal_heart_head", R.mipmap.personal_image_1);
                    map.put("personal_heart_id", temp.get(i).get("nickname").toString());
                    map.put("Account_id",temp.get(i).get("id").toString());
                    map.put("head_picture",temp.get(i).get("head_picture").toString());
                    arrayList.add(map);
                }
                SharedPreferences sharedPreferences = getSharedPreferences(MainActivity.PREFERENCE_NAME,MainActivity.MODE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                final String username = sharedPreferences.getString("username","1");
                pla=new personal_heart_adapter(Personal_heart.this,arrayList,username,1);
                list.setAdapter(pla);
                list.setFocusable(false);
            } else {
                Toast.makeText(Personal_heart.this, "获取失败！",Toast.LENGTH_SHORT).show();
            }
        };
    };
}
